package com.ruoyi.project.baozhi.baozhi.controller;

import java.util.List;
import java.util.UUID;

import com.ruoyi.project.baozhi.box.service.BoxService;
import com.ruoyi.project.baozhi.dept.domain.OrgDept;
import com.ruoyi.project.baozhi.dept.service.IOrgDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.baozhi.baozhi.domain.Baozhi;
import com.ruoyi.project.baozhi.baozhi.service.IBaozhiService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * 报纸Controller
 * 
 * @author ruoyi
 * @date 2021-12-06
 */
@Controller
@RequestMapping("/baozhi/baozhi")
public class BaozhiController extends BaseController
{
    private String prefix = "baozhi/baozhi";

    @Autowired
    private IBaozhiService baozhiService;
    @Autowired
    private BoxService boxService;

    @Autowired
    private IOrgDeptService orgDeptService;
    @RequiresPermissions("baozhi:baozhi:view")
    @GetMapping()
    public String baozhi()
    {
        return prefix + "/baozhi";
    }

    @RequiresPermissions("baozhi:baozhi:view")
    @GetMapping("/fenfa")
    public ModelAndView fenfa()
    {
        ModelAndView model = new ModelAndView(prefix + "/fenfa");
        return model;
    }

    /**
     * 查询报纸列表
     */
    @RequiresPermissions("baozhi:baozhi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Baozhi baozhi)
    {
        startPage();
        List<Baozhi> list = baozhiService.selectBaozhiList(baozhi);
        return getDataTable(list);
    }

    @RequiresPermissions("baozhi:baozhi:list1")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(Baozhi baozhi)
    {
        startPage();
        List<Baozhi> list = baozhiService.selectBaozhiList1(baozhi);
        return getDataTable(list);
    }

    /**
     * 导出报纸列表
     */
    @RequiresPermissions("baozhi:baozhi:export")
    @Log(title = "报纸", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Baozhi baozhi)
    {
        List<Baozhi> list = baozhiService.selectBaozhiList(baozhi);
        ExcelUtil<Baozhi> util = new ExcelUtil<Baozhi>(Baozhi.class);
        return util.exportExcel(list, "报纸数据");
    }

    /**
     * 新增报纸
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报纸
     */
    @RequiresPermissions("baozhi:baozhi:add")
    @Log(title = "报纸", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Baozhi baozhi)
    {
        baozhi.setId(UUID.randomUUID().toString().replace("-","").substring(0,8));

        return toAjax(baozhiService.insertBaozhi(baozhi));
    }

    /**
     * 修改报纸
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Baozhi baozhi = baozhiService.selectBaozhiById(id);
        mmap.put("baozhi", baozhi);
        return prefix + "/edit";
    }

    /**
     * 修改保存报纸
     */
    @RequiresPermissions("baozhi:baozhi:edit")
    @Log(title = "报纸", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Baozhi baozhi)
    {
        return toAjax(baozhiService.updateBaozhi(baozhi));
    }

    /**
     * 删除报纸
     */
    @RequiresPermissions("baozhi:baozhi:remove")
    @Log(title = "报纸", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baozhiService.deleteBaozhiByIds(ids));
    }

    //将报纸的数量赋给部门内数
    @RequiresPermissions("baozhi:baozhi:fenfabaozhi")
    @Log(title = "分发", businessType = BusinessType.DELETE)
    @PostMapping( "/fenfabaozhi")
    @ResponseBody
    public int fenfabaozhi(String name,String year)
    {
        Baozhi baozhi=new Baozhi();
        baozhi.setName(name);
        baozhi.setYear(year);
        baozhiService.baozhishuliang(baozhi);

        OrgDept orgDept=new OrgDept();
        List<OrgDept> list = orgDeptService.selectOrgDeptList(orgDept);
        return boxService.bumenshuaxin_shu(list);
    }
}
